package fms.webservice;

import fms.xml.*;
import org.apache.log4j.Logger;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;

// 如果调用下面方法时，字符串和实际类不对应，就会出现类似NULL的异常
public class XmlUtil {
    public static Logger logger = Logger.getLogger(XmlUtil.class);
    
    // Begin-1、风温“请求”数据“序列化”
    public static String marshallWindRequest(WindRequestXML windRequestXML) {
        try{
            JAXBContext context = JAXBContext.newInstance(WindRequestXML.class);
            Marshaller ms =context.createMarshaller();
            ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            //ms.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
            //ms.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
            StringWriter writer = new StringWriter();
            //writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            ms.marshal(windRequestXML,writer);
            return writer.toString();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }
    // 1、风温“请求”数据“反序列化”
    public static WindRequestXML unMarshallWindRequest(String xmlString) {
        try{
            JAXBContext context = JAXBContext.newInstance(WindRequestXML.class);
            Unmarshaller um =context.createUnmarshaller();
            StringReader reader = new StringReader(xmlString);
            WindRequestXML a = (WindRequestXML)um.unmarshal(reader);
            return a;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }
    // 1、风温“响应”数据“序列化”
    public static String marshallWindResponse(WindResponseXML windResponseXML) {
        try{
            JAXBContext context = JAXBContext.newInstance(WindResponseXML.class);
            Marshaller ms =context.createMarshaller();
            ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            //ms.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
            //ms.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
            StringWriter writer = new StringWriter();
            //writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            ms.marshal(windResponseXML,writer);
            return writer.toString();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }
    // End-1、风温“响应”数据“反序列化”
    public static WindResponseXML unMarshallWindResponse(String xmlString) {
        try{
            System.out.println(xmlString);
            JAXBContext context = JAXBContext.newInstance(WindResponseXML.class);
            Unmarshaller um =context.createUnmarshaller();
            StringReader reader = new StringReader(xmlString);
            WindResponseXML a = (WindResponseXML)um.unmarshal(reader);
            return a;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }

    // Begin-2、位置“请求”数据“序列化”
    public static String marshallPosRequest(PosRequestXML posRequestXML) {
        try{
            JAXBContext context = JAXBContext.newInstance(PosRequestXML.class);
            Marshaller ms =context.createMarshaller();
            ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            //ms.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
            //ms.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
            StringWriter writer = new StringWriter();
            //writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            ms.marshal(posRequestXML,writer);
            return writer.toString();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }
    // 2、位置“请求”数据“反序列化”
    public static PosRequestXML unMarshallPosRequest(String xmlString) {
        try{
            JAXBContext context = JAXBContext.newInstance(PosRequestXML.class);
            Unmarshaller um =context.createUnmarshaller();
            StringReader reader = new StringReader(xmlString);
            PosRequestXML a = (PosRequestXML)um.unmarshal(reader);
            return a;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }
    // 2、位置“响应”数据“序列化”
    public static String marshallPosResponse(PosResponseXML posResponseXML) {
        try{
            JAXBContext context = JAXBContext.newInstance(PosResponseXML.class);
            Marshaller ms =context.createMarshaller();
            ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            //ms.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
            //ms.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
            StringWriter writer = new StringWriter();
            //writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            ms.marshal(posResponseXML,writer);
            return writer.toString();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }
    // End-2、位置“响应”数据“反序列化”
    public static PosResponseXML unMarshallPosResponse(String xmlString) {
        try{
            JAXBContext context = JAXBContext.newInstance(PosResponseXML.class);
            Unmarshaller um =context.createUnmarshaller();
            StringReader reader = new StringReader(xmlString);
            PosResponseXML a = (PosResponseXML)um.unmarshal(reader);
            return a;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }

    // Begin-4、自动位置“请求”数据“序列化”
    public static String marshallAutoPosRequest(AutoPosRequestXML autoPosRequestXML) {
        try{
            JAXBContext context = JAXBContext.newInstance(AutoPosRequestXML.class);
            Marshaller ms =context.createMarshaller();
            ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            StringWriter writer = new StringWriter();
            ms.marshal(autoPosRequestXML,writer);
            return writer.toString();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }
    // 4、自动位置“请求”数据“反序列化”
    public static AutoPosRequestXML unMarshallAutoPosRequest(String xmlString) {
        try{
            JAXBContext context = JAXBContext.newInstance(AutoPosRequestXML.class);
            Unmarshaller um =context.createUnmarshaller();
            StringReader reader = new StringReader(xmlString);
            AutoPosRequestXML a = (AutoPosRequestXML)um.unmarshal(reader);
            return a;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }
    // 4、风温“响应”数据“序列化”
    public static String marshallAutoPosResponse(AutoPosResponseXML autoPosResponseXML) {
        try{
            JAXBContext context = JAXBContext.newInstance(AutoPosResponseXML.class);
            Marshaller ms =context.createMarshaller();
            ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            StringWriter writer = new StringWriter();
            ms.marshal(autoPosResponseXML,writer);
            return writer.toString();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }
    // End-4、风温“响应”数据“反序列化”
    public static AutoPosResponseXML unMarshallAutoPosResponse(String xmlString) {
        try{
            System.out.println(xmlString);
            JAXBContext context = JAXBContext.newInstance(AutoPosResponseXML.class);
            Unmarshaller um =context.createUnmarshaller();
            StringReader reader = new StringReader(xmlString);
            AutoPosResponseXML a = (AutoPosResponseXML)um.unmarshal(reader);
            return a;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }

    // Begin-3、“请求”数据“序列化”
    public static String marshallRequest(fms.webservice.Request request) {
        try{
            JAXBContext context = JAXBContext.newInstance(fms.webservice.Request.class);
            Marshaller ms =context.createMarshaller();
            ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            //ms.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
            //ms.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
            StringWriter writer = new StringWriter();
            //writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            ms.marshal(request,writer);
            return writer.toString();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }
    // 3、“请求”数据“反序列化”
    public static fms.webservice.Request unMarshallRequest(String xmlString) {
        try{
            JAXBContext context = JAXBContext.newInstance(fms.webservice.Request.class);
            Unmarshaller um =context.createUnmarshaller();
            StringReader reader = new StringReader(xmlString);
            fms.webservice.Request a = (Request)um.unmarshal(reader);
            return a;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }
    // 3、“响应”数据“序列化”
    public static String marshallResponse(Response response) {
        try{
            JAXBContext context = JAXBContext.newInstance(Response.class);
            Marshaller ms =context.createMarshaller();
            ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            //ms.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
            //ms.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
            StringWriter writer = new StringWriter();
            //writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            ms.marshal(response,writer);
            return writer.toString();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }
    // End-3、“响应”数据“反序列化”
    public static Response unMarshallResponse(String xmlString) {
        try{
            JAXBContext context = JAXBContext.newInstance(PosResponseXML.class);
            Unmarshaller um =context.createUnmarshaller();
            StringReader reader = new StringReader(xmlString);
            Response a = (Response)um.unmarshal(reader);
            return a;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }

    // Begin-4、位置报转发“请求”数据“序列化”
    public static String marshallInnerRequest(PosRequestInnerXML request) {
        try{
            JAXBContext context = JAXBContext.newInstance(PosRequestInnerXML.class);
            Marshaller ms =context.createMarshaller();
            ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            //ms.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
            //ms.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
            StringWriter writer = new StringWriter();
            //writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            ms.marshal(request,writer);
            return writer.toString();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }
    // End-4、位置报转发“请求”数据“反序列化”
    public static PosRequestInnerXML unMarshallInnerRequest(String xmlString) {
        try{
            JAXBContext context = JAXBContext.newInstance(PosRequestInnerXML.class);
            Unmarshaller um =context.createUnmarshaller();
            StringReader reader = new StringReader(xmlString);
            PosRequestInnerXML a = (PosRequestInnerXML)um.unmarshal(reader);
            return a;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }


    // Begin-1、飞行计划“请求”数据“序列化”
    public static String marshallFlpRequest(FlpRequestXML flpRequestXML) {
        try{
            JAXBContext context = JAXBContext.newInstance(FlpRequestXML.class);
            Marshaller ms =context.createMarshaller();
            ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            //ms.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
            //ms.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
            StringWriter writer = new StringWriter();
            //writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            ms.marshal(flpRequestXML,writer);
            return writer.toString();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }
    // 1  飞行计划“请求”数据“反序列化”
    public static FlpRequestXML unMarshallFlpRequest(String xmlString) {
        try{
            JAXBContext context = JAXBContext.newInstance(FlpRequestXML.class);
            Unmarshaller um =context.createUnmarshaller();
            StringReader reader = new StringReader(xmlString);
            FlpRequestXML a = (FlpRequestXML)um.unmarshal(reader);
            return a;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }

    // Begin-1、飞行计划“回复”数据“序列化”
    public static String marshallFlpResponse(FlpResponseXML flpResponseXML) {
        try{
            JAXBContext context = JAXBContext.newInstance(FlpResponseXML.class);
            Marshaller ms =context.createMarshaller();
            ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            //ms.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
            //ms.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
            StringWriter writer = new StringWriter();
            //writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            ms.marshal(flpResponseXML,writer);
            return writer.toString();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }
    // 1  飞行计划“回复”数据“反序列化”
    public static FlpResponseXML unMarshallFlpResponse(String xmlString) {
        try{
            JAXBContext context = JAXBContext.newInstance(FlpResponseXML.class);
            Unmarshaller um =context.createUnmarshaller();
            StringReader reader = new StringReader(xmlString);
            FlpResponseXML a = (FlpResponseXML)um.unmarshal(reader);
            return a;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }




}
